package algorithm;

public class leetcode_0567 {
    public boolean checkInclusion(String s1, String s2) {
        int[] num = new int[26];
        for(int i=0;i<s1.length();++i){
            num[s1.charAt(i)-'a']+=1;
        }
        int left = 0;
        int right =0;
        while(right<s2.length()){
            if (right<s2.length()){
                num[s2.charAt(right)-'a']-=1;
                right++;
            }
            if(right-left>s1.length()){
                num[s2.charAt(left)-'a']+=1;
                left++;
            }
            if(right-left==s1.length()){
                if(isZero(num)){
                    return true;
                }
            }
        }
        return false;
    }

    public boolean isZero(int[] num){
        for(int i=0;i<26;++i){
            if(num[i]!=0) return false;
        }
        return true;
    }
}
